package com.iiintelife.demo.generator;

/**
 * @Author: liuchengcheng
 * @Date: 2021/9/27 12:22
 */
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;

public class CodeGenerator {
    /**
     * <p>
     * ???????
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("???" + tip + "?");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("??????" + tip + "?");
    }

    public static void main(String[] args) {
        // ?????
        AutoGenerator mpg = new AutoGenerator();
        // ?? freemarker ????? Veloctiy
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        String projectPath = System.getProperty("user.dir");
        // ????
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "/src/main/java");	//?????????
        gc.setAuthor("lcc");					//??
        gc.setFileOverride(true);				//???????? ????false
        gc.setOpen(false);						//???????? ???:true

        gc.setSwagger2(true);					//?? swagger2 ?? ??false
        gc.setBaseColumnList(true);				//?? baseColumnList ??false
        gc.setBaseResultMap(true);				//?? BaseResultMap ??false
        gc.setEntityName("%sEntity");			//??????  ????null ???%sEntity ?? UserEntity
        gc.setMapperName("%sMapper");			//mapper ???? ????null ???%sDao ?? UserDao
        gc.setXmlName("%sMapper");				//Mapper xml ????   ????null ???%sDao ?? UserDao.xml
        gc.setServiceName("%sService");			//service ????   ????null ???%sBusiness ?? UserBusiness
        gc.setServiceImplName("%sServiceImpl");	//service impl ????  ????null ???%sBusinessImpl ?? UserBusinessImpl
        gc.setControllerName("%sController");	//controller ????    ????null ???%sAction ?? UserAction
        mpg.setGlobalConfig(gc);

        // ?????
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);		//?????	?????????????????
        dsc.setUrl("jdbc:mysql://172.18.0.121:13306/sharding_db?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        // ???
        PackageConfig pc = new PackageConfig();
//	        pc.setModuleName(scanner("???"));
        pc.setParent("com.iiintelife.demo");
        mpg.setPackageInfo(pc);
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);	//??????
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//????????????????, ????? naming ??
//	        strategy.setCapitalMode(true);			// ?????? ORACLE ??
        strategy.setTablePrefix("t_");		//???
//	        strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");	//??????Entity???????
//	        strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); 	//??????????
        strategy.setEntityLombokModel(true);	//???????lombok????? false
        strategy.setRestControllerStyle(true);	//?? @RestController ???
//	        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");	//??????Controller???????
        strategy.setInclude(scanner("??"));		//?????????????????exclude??????
//	        strategy.setInclude(new String[] { "user" }); // ???????????
//	        strategy.setExclude(new String[]{"test"}); // ??????
        strategy.setControllerMappingHyphenStyle(true);	//??????
        //strategy.setTablePrefix(pc.getModuleName() + "_");	//??????????????
        strategy.setEntityBooleanColumnRemoveIsPrefix(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
